package com.ruoyi.center.demo.apiimpl;

import cn.hutool.core.util.IdUtil;
import com.ruoyi.center.demo.api.DemoApi;
import com.ruoyi.center.demo.convert.DemoConverter;
import com.ruoyi.center.demo.api.dto.DemoDto;
import com.ruoyi.center.demo.service.DemoService;
import com.ruoyi.common.core.dto.CommonResultDto;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;

/**
 * demo接口实现
 *
 * @author ruoyi
 * @date 2022-02-15 10:59:29
 */
@RequestMapping("/v1/demo")
@RestController
@DubboService(group = "${dubbo.application.name}",protocol = "${dubbo.protocol.name}",
        version = "${dubbo.service.version}")
public class DemoApiImpl implements DemoApi {
    @Autowired
    private DemoService demoService;

    /**
     * 新增
     *
     * @param demoDto
     * @return
     */
    @Override
    public CommonResultDto<Long> create(@RequestBody DemoDto demoDto) {
        Long id = demoDto.getId();
        if (id == null) {
            id = IdUtil.getSnowflakeNextId();
        }
        demoDto.setId(id);
        demoService.save(DemoConverter.dto2Do(demoDto));
        return CommonResultDto.success(id);
    }

    /**
     * 修改
     *
     * @param demoDto
     * @return
     */
    @Override
    public CommonResultDto<Boolean> modify(@RequestBody DemoDto demoDto) {
        return CommonResultDto.success(demoService.updateById(DemoConverter.dto2Do(demoDto)));
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @Override
    public CommonResultDto<Boolean> remove(@PathVariable Long [] ids) {
        return CommonResultDto.success(demoService.removeBatchByIds(Arrays.asList(ids)));
    }
}
